package com.kang.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.Supplier;
import com.kang.service.AssistService;
import com.kang.service.SupplierService;
import com.vonechina.entity.page.SupplierPage;
/**
 * 供应商管理控制层
 * @author zs614
 *
 */
@Controller
@RequestMapping("supplier")
@SessionAttributes({ "supplierPage" })
public class SupplierController extends com.kang.controller.Controller {
	/**
	 * 供应商服务层
	 */
	@Resource
	private SupplierService ss;
	/**
	 * 通用服务层
	 */
	@Resource
	private AssistService assistService;

	/**
	 * 分页查看供应商接口
	 * @param session
	 * @param model
	 * @param supplierPage 搜索条件自动封装
	 * @return
	 */
	@RequestMapping("listSupplier")
	public String listSupplier(HttpSession session, Model model, SupplierPage supplierPage) {
		initRight(session);
		if (checkRight("查看供应商") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isAdd", checkRight("新增供应商") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑供应商") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除供应商") == SUCCESS);
		model.addAttribute("isUploadAnnex", checkRight("上传供应商附件") == SUCCESS);
		model.addAttribute("isDownloadAnnex", checkRight("下载供应商附件") == SUCCESS);
		model.addAttribute("isDeleteAnnex", checkRight("删除供应商附件") == SUCCESS);
		model.addAttribute("supplierList", ss.listSupplier(supplierPage));
		model.addAttribute("nationalities", assistService.queryAreas(0));
		supplierPage.setPageUrl("supplier/listSupplier.do");
		model.addAttribute("page", supplierPage);
		return "basicData/supplierManage/listSupplier";
	}

	/**
	 * 通过ID获取供应商信息
	 * @param supplierid
	 * 供应商ID
	 * @return
	 */
	@RequestMapping("findSupplierById")
	@ResponseBody
	public String findSupplierById(Integer supplierid) {
		Supplier supplier = ss.selectByPrimaryKey(supplierid);
		return JSON.toJSONString(supplier);
	}

	/**
	 * 新增供应商
	 * @param session
	 * @param supplier 供应商实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addsupplier(HttpSession session, Supplier supplier) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增供应商") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		supplier.setSuppliercode(ss.generateSupplierCode());
		supplier.setCreatetime(new Date());
		supplier.setFinalprocessingtime(new Date());
		supplier.setCreatepersonid((Integer) session.getAttribute("userID"));
		supplier.setLastpersonid((Integer) session.getAttribute("userID"));
		int a = ss.insertSelective(supplier);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}
	/**
	 * 编辑供应商
	 * @param session
	 * @param supplier 供应商实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("update")
	@ResponseBody
	public String updatesupplier(HttpSession session, Supplier supplier) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑供应商") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		supplier.setFinalprocessingtime(new Date());
		int a = ss.updateByPrimaryKeySelective(supplier);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}
	/**
	 * 删除供应商
	 * @param session
	 * @param supplierid 供应商ID
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deletesupplier(HttpSession session, Integer supplierid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除供应商") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = ss.deleteByPrimaryKey(supplierid);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	public SupplierService getSs() {
		return ss;
	}

	public void setSs(SupplierService ss) {
		this.ss = ss;
	}

}
